package com.yf.cl.initializaton;

/*
虚拟机必须保证一个类的clinit()方法在多线程下被同步加锁
 */
public class ClinitThreadTest {
    public static void main(String[] args) {
        Runnable r = () -> {
            System.out.println(Thread.currentThread().getName() + "开始");
            DeadThread deadThread = new DeadThread();
            System.out.println(Thread.currentThread().getName() + "结束");
        };

        new Thread(r, "线程1").start();
        new Thread(r, "线程2").start();
    }
}


class DeadThread {
    // 多线程下该静态代码块只执行一次，就符合预期
    static {
        if (true) {
            System.out.println(Thread.currentThread().getName() + "初始化当前类");
            while (true) {}
        }
    }
}